<?php
// +----------------------------------------------------------------------
// | 通用PHP框架 [ ThinkPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 郑州智众 [Paulxun@qq.com]
// +----------------------------------------------------------------------
// | 官方网站: https://www.zzmind.com
// +----------------------------------------------------------------------

namespace app\index\controller;

use app\common\controller\BaseController;
use util\Wxsdk;

/**
 * 前台公共控制器
 * @package app\index\controller
 */
class Home extends BaseController
{
    /**
     * 初始化方法
     * @author PaulXun <paulxun@qq.com>
     */
    protected function initialize()
    {
        // 系统开关
        if (!config('app.web_site_status')) {
            $this->error('站点已经关闭，请稍后访问~');
        }
		
		// 获取浏览器类型
		$ua = get_ua();
		$this->assign('ua', $ua);
		
		// 判断登录
		$uid = is_login();
		if(!$uid){
			cookie('__backurl__', $_SERVER['REQUEST_URI']);
			if ($ua == 'ali'){
				$this->redirect(url('user/member/alilogin'));
			} else {
				$this->redirect(url('user/member/wxlogin'));
			}
		}
		
		if($ua == 'ali'){
			// 支付宝SDK支持
			$jssdk = $this->getAliSdk();
			if($jssdk['code'] == 1) $this->assign('jssdk', $jssdk);
		}else{
			// 微信SDK支持
			$jssdk = $this->getWxSdk();
			if($jssdk['code'] == 1) $this->assign('jssdk', $jssdk);
		}
		// 获取所有导航
		$nav = \app\cms\model\Nav::getNav();
		$this->assign('nav', $nav);
		// 获取模型
		$this->assign('active', app('http')->getName());
    }
	// 发送微信模板消息
	protected function template($data)
	{
	    $wxsdk = new Wxsdk;
		return $wxsdk->template($data);
	}
	
	// 获取access_token
	protected function getAccessToken()
	{
	    $wxsdk = new Wxsdk;
		return $wxsdk->getAccessToken();
	}
	
	// 获取分享参数
	protected function getWxSdk($url = null)
	{
	    $wxsdk = new Wxsdk;
		return $wxsdk->getWxSdk($url);
	}
}
